package com.androturka;

import java.util.ArrayList;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class ConfigurationManager{
	
	EarthQuakeDB DB;
	private SQLiteDatabase dbObject;
	
	boolean		workAtBackGround		= true;
	boolean 	vibrationNotification 	= true;
	boolean 	ringNotification 		= true;
	boolean 	barNotification 		= true;
	int			workingFrequency 		= 0;
	String 		latestEarthQuakeTime	= null;
	
	ArrayList<Filter> 	filters = new ArrayList<Filter>();
	
	public ConfigurationManager( Context context ){
		DB = new EarthQuakeDB(context);
		dbObject = DB.getWritableDatabase();		
		DB.onCreate(dbObject);
	
		vibrationNotification 	= DB.getVibrationNotification();
		ringNotification 		= DB.getRingNotification();
		//barNotification 		= DB.getBarNotification();
		//workAtBackGround		= DB.getWorkAtBackGround();
		latestEarthQuakeTime	= DB.getLatestEarthQuakeTime();
		filters					= DB.getFilters();
		
		/*
		deleteAllFilter();
		deleteParameters();
		*/
	}
	
	
	public boolean getWorkAtBackGround() {
		return workAtBackGround;
	}
	public void updateWorkAtBackGround(boolean workAtBackGround) {
		DB.updateWorkAtBackGround(workAtBackGround);
	}
	public boolean getVibrationNofitication() {
		return vibrationNotification;
	}
	public void updateVibrationNotification(boolean vibrationNotification) {
		DB.updateVibrationNotification(vibrationNotification);
	}
	public boolean getRingNotification() {
		return ringNotification;
	}
	public void updateRingNotificaiton(boolean ringNotification) {
		DB.updateRingNotificaiton(ringNotification);
	}	
	public boolean getBarNotification() {
		return barNotification;
	}
	public void updateBarNotification(boolean barNotification) {
		DB.updateBarNotification(barNotification);
	}	
	public int getWorkingFrequency() {
		return workingFrequency;
	}
	public String getLatestEarthQuakeTime() {
		return latestEarthQuakeTime;
	}
	public void updateLatestEarthQuakeTime(String latestEarthQuakeTime) {
		DB.updateLatestEarthQuakeTime(latestEarthQuakeTime);
	}	
	public ArrayList<Filter> getFilters() {
		return filters;
	}
	public Filter getFilter(int filterid) {
//		if ( filters != null )
//			return filters.get(filterid);

		if (filters.isEmpty() == false)
			return filters.get(filterid);
		
		return null;
	}

	public void updateFilter(Filter filter)
	{
		DB.updateFilter(filter);
	}
	public void deleteFilter(int filterid)
	{
		DB.deleteFilter(filterid);
	}
	public void insertFilter( double magnitude, int distance, int cityid)
	{
		DB.insertFilter( magnitude, distance, cityid );
	}
	public void deleteAllFilter()
	{
		DB.deleteAllFilter();
	}
	public void insertParameters( int workingFrequency, int barNotification, int ringNotification, int vibrationNotification, String latestEarthQuakeTime )
	{
		DB.insertParameters( workingFrequency, barNotification, ringNotification, vibrationNotification, latestEarthQuakeTime);
	}
	public void deleteParameters()
	{
		DB.deleteParameters();
	}

	public void cleanup() {
		DB.close();
		dbObject.close();

	}

	public String getFilterURL() {
		String url = null;
		String filterUrl = null;
		// TODO Auto-generated method stub
		for (int i=0;i<filters.size();i++){
			//"&city[0]=6&distance[0]=100&magnitude[0]=3&city[1]=55&distance[1]=100&magnitude[1]=3&city[2]=43&distance[2]=100&magnitude[2]=3&city[3]=65&distance[3]=100&magnitude[3]=3"
			filterUrl = "&city["+i+"]="+filters.get(i).cityid + "&distance["+i+"]="+filters.get(i).distance +"&magnitude["+i+"]="+filters.get(i).magnitude;
			//Log.d("test", filterUrl);
			if ( url == null )
				url = filterUrl;
			else
				url = url.concat(filterUrl);
		} 
		
		//Log.d("test", url);
		return url;
	}
}
